<?php

namespace app\admin\service;

use app\admin\model\BoxAdMedia;
use app\admin\model\BoxStatisticsAdRemainLog;
use app\admin\model\Package;
use app\admin\model\UserPromote;
use think\Db;

class BoxStatisticsAdRemainService extends BaseService
{

    /**
     * @Notes:广告投放统计
     * @Interface getList
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['media_id']) && !empty($param['media_id'])) {
            $map[] = ['media_id', '=', $param['media_id']];
        }

        if (isset($param['promote_id']) && !empty($param['promote_id'])) {
            $map[] = ['promote_id', '=', $param['promote_id']];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        $list = (new BoxStatisticsAdRemainLog())
            ->field('day_time,platform,promote_id,media_id,package_id,account_num,d2,d3,d4,d5,d6,d7,d14,d30,d60,d90,d180')
            ->where($map)
            ->order('day_time', 'desc')
            ->select()->toArray();
        //获取数据总数
        $count = count($list);

        //媒体
        $promoteData = UserPromote::field(['id', 'realname'])->select()->toArray();
        $promoteData = array_column($promoteData, 'realname', 'id');

        //产品
        $packageData            = Package::field(['id', 'package_name'])->select()->toArray();
        $packageData            = array_column($packageData, 'package_name', 'id');
        $packageData['9999997'] = '千年游戏盒子pc';
        $packageData['9999998'] = '千年游戏盒子ios';
        $packageData['9999999'] = '千年游戏盒子android';

        //投放渠道
        $mediaData = BoxAdMedia::field(['id', 'name'])->select()->toArray();
        $mediaData = array_column($mediaData, 'name', 'id');

        if (!empty($list)) {
            $account_num_all = 0;
            $d2_all          = 0;
            $d3_all          = 0;
            $d4_all          = 0;
            $d5_all          = 0;
            $d6_all          = 0;
            $d7_all          = 0;
            $d14_all         = 0;
            $d30_all         = 0;
            $d60_all         = 0;
            $d90_all         = 0;
            $d180_all        = 0;
            foreach ($list as $key => &$item) {
                $account_num_all += $item['account_num'];
                if (!empty($item['account_num'])) {
                    $d2_all   += round($item['d2'] / $item['account_num'] * 100);
                    $d3_all   += round($item['d3'] / $item['account_num'] * 100);
                    $d4_all   += round($item['d4'] / $item['account_num'] * 100);
                    $d5_all   += round($item['d5'] / $item['account_num'] * 100);
                    $d6_all   += round($item['d6'] / $item['account_num'] * 100);
                    $d7_all   += round($item['d7'] / $item['account_num'] * 100);
                    $d14_all  += round($item['d14'] / $item['account_num'] * 100);
                    $d30_all  += round($item['d30'] / $item['account_num'] * 100);
                    $d60_all  += round($item['d60'] / $item['account_num'] * 100);
                    $d90_all  += round($item['d90'] / $item['account_num'] * 100);
                    $d180_all += round($item['d180'] / $item['account_num'] * 100);
                }

                $item['media_name']   = $mediaData[$item['media_id']] ?? '';
                $item['promote_name'] = $promoteData[$item['promote_id']] ?? '';
                $item['package_name'] = $packageData[$item['package_id']] ?? '';
                $item['day_time']     = date("Y-m-d", strtotime($item['day_time']));
                $item['d2']           = empty($item['account_num']) ? '0%' : round($item['d2'] / $item['account_num'] * 100) . "%";
                $item['d3']           = empty($item['account_num']) ? '0%' : round($item['d3'] / $item['account_num'] * 100) . "%";
                $item['d4']           = empty($item['account_num']) ? '0%' : round($item['d4'] / $item['account_num'] * 100) . "%";
                $item['d5']           = empty($item['account_num']) ? '0%' : round($item['d5'] / $item['account_num'] * 100) . "%";
                $item['d6']           = empty($item['account_num']) ? '0%' : round($item['d6'] / $item['account_num'] * 100) . "%";
                $item['d7']           = empty($item['account_num']) ? '0%' : round($item['d7'] / $item['account_num'] * 100) . "%";
                $item['d14']          = empty($item['account_num']) ? '0%' : round($item['d14'] / $item['account_num'] * 100) . "%";
                $item['d30']          = empty($item['account_num']) ? '0%' : round($item['d30'] / $item['account_num'] * 100) . "%";
                $item['d60']          = empty($item['account_num']) ? '0%' : round($item['d60'] / $item['account_num'] * 100) . "%";
                $item['d90']          = empty($item['account_num']) ? '0%' : round($item['d90'] / $item['account_num'] * 100) . "%";
                $item['d180']         = empty($item['account_num']) ? '0%' : round($item['d180'] / $item['account_num'] * 100) . "%";
            }
            $list[$count]['day_time']     = "汇总";
            $list[$count]['media_name']   = "-";
            $list[$count]['package_name'] = "-";
            $list[$count]['platform']     = "-";
            $list[$count]['promote_name'] = "-";
            $list[$count]['account_num']  = $account_num_all;
            $list[$count]['d2']           = round($d2_all / $count) . "%";
            $list[$count]['d3']           = round($d3_all / $count) . "%";
            $list[$count]['d4']           = round($d4_all / $count) . "%";
            $list[$count]['d5']           = round($d5_all / $count) . "%";
            $list[$count]['d6']           = round($d6_all / $count) . "%";
            $list[$count]['d7']           = round($d7_all / $count) . "%";
            $list[$count]['d14']          = round($d14_all / $count) . "%";
            $list[$count]['d30']          = round($d30_all / $count) . "%";
            $list[$count]['d60']          = round($d60_all / $count) . "%";
            $list[$count]['d90']          = round($d90_all / $count) . "%";
            $list[$count]['d180']         = round($d180_all / $count) . "%";
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

}
